var OAuthLogin = (function ($){
	
	return {
		
		wait: 60,
		
		showResetPassword: function () {
			
			
			$('#resetForm').animate({
				height: 'toggle',
				opacity: 'toggle'
			}, 'slow');
			
			$('.login-form').animate({
				height: 'toggle',
				opacity: 'hide'
			}, 'slow');
		},
		
		showLogin: function() {
			$('#smsLoginForm').animate({
				height: 'toggle',
				opacity: 'toggle'
			}, 'slow');
			
			$('.login-form').animate({
				height: 'toggle',
				opacity: 'hide'
			}, 'slow');
		},
		
		resetPasswordBack: function() {
			$('#resetForm').animate({
				height: 'toggle',
				opacity: 'toggle'
			}, 'slow');
			
			$('#smsLoginForm').hide();
			
			$('.login-form').animate({
				height: 'toggle',
				opacity: 'show'
			}, 'slow');
		},
		
		loginBack: function() {
		
			$('#resetForm').hide();
			
			$('#smsLoginForm').animate({
				height: 'toggle',
				opacity: 'toggle'
			}, 'slow');
			
			$('.login-form').animate({
				height: 'toggle',
				opacity: 'show'
			}, 'slow');
			
		},
		
		countDown: function (obj) {
			var me = this;
			
			if (me.wait == 0) {
				$(obj).attr('onclick', 'countDown(this)');
	            $(obj).html('<a href="#" class="yzm1" onclick="OAuthLogin.sendMessage(this)"><span class="yzm active">点击获取</span></a>');  
	            me.wait = 60;  
	        } else {  
				$(obj).attr('onclick', '');
	            $(obj).html('<span class="yzm">' + me.wait + '秒后重发</span>');
	            me.wait--;  
	            setTimeout(function() {  
	                me.countDown(obj)  
	            },  
	            1000)  
	        }
		
		},
		
		countDown1: function (obj) {
			var me = this;
			
			if (me.wait == 0) {
				$(obj).attr('onclick', 'countDown1(this)');
	            $(obj).html('<a href="#" class="yzm1" onclick="OAuthLogin.sendMessageLogin(this)"><span class="yzm active">点击获取</span></a>');  
	            me.wait = 60;  
	        } else {  
				$(obj).attr('onclick', '');
	            $(obj).html('<span class="yzm">' + me.wait + '秒后重发</span>');
	            me.wait--;  
	            setTimeout(function() {  
	                me.countDown1(obj)  
	            },  
	            1000)  
	        }
		
		},
		
		sendMessage : function(obj){
			
			var data = {
					userAccount : $('#userAccount').val(),
					mobilePhone : $('#mobilePhone').val(),
					flag : "resetPassword",
			}
			jQuery.ajax({
				type : "POST",
				url : BASE_PATH + "oauth/sendResetPasswordMessage.action",
				data:data,
				async : false,
				//返回值
				success : function(res) {
					if(res.success == true){
						$('#message').html('');
						OAuthLogin.countDown(obj);
					}else{
						$('#message').html(res.message);
					}
				}
			});
			
		},
		
		resetPassword : function(){
			var data = {
					userAccount : $('#userAccount').val(),
					mobilePhone : $('#mobilePhone').val(),
					code : $('#code').val(),
			}
			jQuery.ajax({
				type : "POST",
				url :  BASE_PATH + "oauth/ResetPassword.action",
				data:data,
				async : false,
				//返回值
				success : function(res) {
					if(res.success == true){
						$('#message').html('');
						alert("密码已重置，新密码将发送至你的手机，请注意查收。")
						$('#myModalContainer').modal("hide");
					}else{
						$('#message').html(res.message);
					}
				}
			});
		},
		
		sendMessageLogin : function(obj){
			
			var data = {
					userAccount : $('#account').val(),
					mobilePhone : $('#phone').val(),
					flag : "login",
			}
			jQuery.ajax({
				type : "POST",
				url : BASE_PATH + "oauth/sendResetPasswordMessage.action",
				data:data,
				async : false,
				//返回值
				success : function(res) {
					if(res.success == true){
						$('#message1').html('');
						OAuthLogin.countDown1(obj);
					}else{
						$('#message1').html(res.message);
					}
				}
			});
			
		},
		
		SMSLogin : function(){
			$('#pageUrl').val(window.location.href);
			$('#smsLoginForm').submit();
		},
		
		initPage : function (){
			var flag = this.GetQueryString("flag");
			if(flag == "error"){
				$("#myModalContainer").modal("show");
				this.showLogin();
				$('#message1').html("账号名、手机号码或验证码错误");
				
				$(".modal-content").children(".close").click(function(){
					
					$("#myModalContainer").modal("hide");
					
				});
				
				$(".modal-backdrop").click(function(){
					
					$("#myModalContainer").modal("hide");
				});
			}
		},
		
		GetQueryString : function (name)
		{
		     var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
		     var r = window.location.search.substr(1).match(reg);
		     if(r!=null)return  unescape(r[2]); return null;
		}
		
	}
})(jQuery);
